//
//  SATabViewController.m
//  SAP
//
//  Created by 中矿科技有限公司 on 17/9/8.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "SATabViewController.h"
#import "SACustomerViewController.h"
#import "SAContractViewController.h"
#import "SAAllocateViewController.h"
#import "SANavigationViewController.h"
#import "SAHomeViewController.h"

@interface SATabViewController ()

@end

@implementation SATabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =SA_AllBackColor;
    
    [self customUI];
    
    // Do any additional setup after loading the view.

}

-(void)customUI{
    [self addLoginedChildViewController:[[SAHomeViewController alloc]init] withTitle:@"首页" image:@"Home_nor" selectedImage:@"Home_highlight"];
    [self addLoginedChildViewController:[[SACustomerViewController alloc]init] withTitle:@"客户" image:@"customer_nor" selectedImage:@"customer_highlight"];
    [self addLoginedChildViewController:[[SAContractViewController alloc]init] withTitle:@"合同" image:@"Contract_nor" selectedImage:@"Contract_highlight"];
    [self addLoginedChildViewController:[[SAAllocateViewController alloc]init] withTitle:@"调拨" image:@"Allocate_nor" selectedImage:@"Allocate_highlight"];

    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorFromHex:@"007aff"];
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
}

// 登陆后的导航控制器
- (void)addLoginedChildViewController:(UIViewController *)childVC withTitle:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    // 设置控制器的标题
    childVC.title = title;
    
    // 设置tabBarItem图片 ,禁用渲染
    UIImage * norimage = [self getImageFromImageInfo:image];
    norimage = [norimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childVC.tabBarItem.image = norimage;
    UIImage * selectedImage1 = [self getImageFromImageInfo:selectedImage];
    selectedImage1 = [selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = selectedImage1;
    
    // 为子控制器包装导航控制器
    
    SANavigationViewController *NaviVC = [[SANavigationViewController alloc]initWithRootViewController:childVC];
    // 添加子控制器
    [self addChildViewController:NaviVC];
}

- (UIImage *)getImageFromImageInfo:(id)imageInfo {
    UIImage *image = nil;
    if ([imageInfo isKindOfClass:[NSString class]]) {
        image = [UIImage imageNamed:imageInfo];
    } else if ([imageInfo isKindOfClass:[UIImage class]]) {
        image = (UIImage *)imageInfo;
    }
    return image;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
